﻿using System;
using System.Collections.Generic;
using System.Linq;
using Coding4Fun.Kinect.WinForm;
using Microsoft.Kinect;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace SpaceKinect
{
    class Player:Ship
    {
        bool lavaCannon;
        //if invulnerabble
        bool invuln;
        //Time that invulnerability started
        double invulnStartTime = 0;
        //Time that last powerup started.
        double lavaCannonStartTime = 0;
        //number of lives
        int lives;

        //Score from shooting targets
        int targetScore = 0;
        //Total score (targets + distance travelled)
        int totalScore = 0;

        //Number of destroyed targets between powerups
        int destroyedTargets = 0;

        //used for calculating explosion sound volume
        bool explos = false;
        Vector3 explosVec;

        //Not the best place for this, but it works)
        //Holds the location where the powerup should be generated
        Vector3 tmpPowerUpLocation;

        public Player(Model m, Vector3 p, Quaternion q)
        {
            lavaCannon = false;
            invuln = false;
            model = m;
            position = p;
            rotation = q;
            lives = 3;
            health = 100;
            bound = new BoundingSphere(position, 0.04f);
        }

        public int TargetScore
        {
            get { return targetScore; }
            set { targetScore = value; }
        }

        public int TotalScore
        {
            get { return totalScore; }
            set { totalScore = value; }
        }

        public int DestroyedTargets
        {
            get { return destroyedTargets; }
            set { destroyedTargets = value; }
        }

        public bool LavaCannon
        {
            get { return lavaCannon; }
            set { lavaCannon = value; }
        }
        public bool Invuln
        {
            get { return invuln; }
            set { invuln = value; }
        }
        public double InvulnStartTime
        {
            get { return invulnStartTime; }
            set { invulnStartTime = value; }
        }
        public double LavaCannonStartTime
        {
            get { return lavaCannonStartTime; }
            set { lavaCannonStartTime = value; }
        }
        public int Lives
        {
            get { return lives; }
            set { lives = value; }
        }

        public bool Explos
        {
            get { return explos; }
            set { explos = value; }
        }

        public Vector3 ExplosVec
        {
            get { return explosVec; }
            set { explosVec = value; }
        }

        public Vector3 TmpPowerUpLocation
        {
            get { return tmpPowerUpLocation; }
            set { tmpPowerUpLocation = value; }
        }

        public void Death(GameTime g, int mapWidth)
        {
            if (lives > 0)
            {
                invuln = true;
                invulnStartTime = g.TotalGameTime.TotalMilliseconds;
                health = 100;
                position = new Vector3(position.X, 2, -(float)mapWidth / 2);
                lives--;
            }
            //else
                
        }

        public override void Draw(Matrix projection, Matrix view)
        {
            Matrix world = Matrix.CreateScale(.0135f, .0135f, .0135f) * Matrix.CreateTranslation(-.045f, 0, 0) * Matrix.CreateRotationY(MathHelper.Pi) * Matrix.CreateRotationZ(MathHelper.Pi) * Matrix.CreateFromQuaternion(rotation) * Matrix.CreateTranslation(position);
            
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;

                    effect.World = world;
                    effect.Projection = projection;
                    effect.View = view;
                }
                mesh.Draw();
            }
        }
    }
}
